var countdown=60;
function set_time(obj) {
    var str = $("#user_phone").val();
    if (str== "" ){
        $("#user_phone").placeholder="手机号不能为空！";
        flag=0;
        document.getElementById("user_phone".placeholder.color="red");
    }else {
        var re = /^1\d{10}$/;
        if (!re.test(str)) {
            flag=1;
        }
        else{
            flag=2;
        }
    }
    if(flag==0){
        $("#user_phone").value = "";
        $("#user_phone").placeholder = "手机号不能为空！";
    }
    if(flag==1){
        document.getElementById("phoneLabel").style.display = "inline";
    }
    if(flag==2){
        var userAccount=$("#user_phone").val();
        $.post("/user/PCaptcha", "userAccount="+userAccount, function (data,status) {
                if (data==1){
                    timeSt(obj);
                }else if(data==2) {
                    alert("该用户已注册");
                }
        });

    }
}

function timeSt(obj) {
    if (countdown == 0) {
        obj.removeAttribute("disabled");
        obj.value="获取验证码";
        countdown = 60;
        return;
    } else {
        obj.setAttribute("disabled", true);
        obj.value="重新发送(" + countdown + ")";
        countdown--;
    }
    setTimeout(function() {
            timeSt(obj)
        }
        ,1000)

}



var flag;
var phoneNum;
function reg1(){
    var str = $("#user_phone").val();
    if (str== "" ){
        user_phone.placeholder="手机号不能为空！";
        flag=0;
        document.getElementById("user_phone".placeholder.color="red");
    }
    else{
        var re = /^1\d{10}$/;
        if (!re.test(str)) {
            flag=1;
        }
        else{
            flag=2;
        }
    }
    if(flag==0){
        $("#user_phone").value = "";
        $("#user_phone").placeholder = "手机号不能为空！";
    }
    if(flag==1){
        $("#phoneLabel").text("手机格式不正确");
        document.getElementById("phoneLabel").style.display = "inline";
    }
    if(flag==2){
        var userAccount=$("#user_phone").val();
        var captcha = $("#captcha").val();
        $.post("/User/Register", "userAccount="+userAccount+"&captcha="+captcha, function (data,status) {
            if (data==0){
                window.location.href = "register2.html";
            } else if (data==1){
                alert("验证码不正确");
            }else if (data==2){
                alert("验证码不正确");
            }else if(data==3){
                alert("超时");
            }else {
                alert("此账号已被注册");
            }
        });
    }
}

function phone_reset(){
    user_phone.placeholder="请输入11位手机号";
}
function password_reset(){
    document.getElementById("passwordLabel").style.display = "none";
    user_password.placeholder="密码(6~16位，字母开头，数字，下划线)";

}

var flag1;
function checkMobile () {
    var str = $("#user_phone").val();
    var str1 = $("#captcha").val();
    if(str1==""){
        if (str== "" ){
            user_phone.placeholder="手机号不能为空！";
            flag=0;
            document.getElementById("user_phone".placeholder.color="red");
        }else {
            var re = /^1\d{10}$/;
            if (!re.test(str)) {
                flag=1;
            }
            else{
                flag=2;
            }
        }
        if(flag==0){
            user_phone.value = "";
            user_phone.placeholder = "手机号不能为空！";
        }
        if(flag==1){
            document.getElementById("phoneLabel").style.display = "inline";
        }
        if(flag==2){
            document.getElementById("phoneLabel").style.display = "none";
            document.getElementById("next").removeAttribute("disabled");
            document.getElementById("next").style.background="#0077E6";
        }
    }
}

function checkPassword(str){
    document.getElementById("passwordLabel").style.display = "none";
    if (str== "" ){
        user_password.placeholder="密码不能为空！";
    }
    else {
        var re = /^[a-zA-Z]\w{5,16}$/;
        if (!re.test(str)) {
            user_password.value = "";
            user_password.placeholder = "密码不合法！";
        }
    }
}

var userAccount;
var password;
var userName;
var identify;
function checkSame(){
    document.getElementById("passwordLabel").style.display = "none";
    var p1=document.getElementById("user_password").value;
    var p2=document.getElementById("user_password_settled").value;
    if(p1==""){
        user_password.placeholder="密码不能为空！";
    }
    if(p1!=p2){
        document.getElementById("passwordLabel").style.display = "inline";
    }
    if(p1==p2&&p1!="") {
        if(document.getElementById("buyer").checked==true){
            identify=0;
            userName=document.getElementById("user_name").value;
            password=document.getElementById("user_password").value;
            $.post("/User/Register2", "userName="+userName+"&userPassword="+password+"&identify="+identify, function (data,status) {
                if (data == 0) {
                    alert("注册失败");
                } else if (data == 2) {
                    alert("请从头开始注册");
                } else {
                    window.location.href = "First_Page.jsp";
                }
            },"json");
        }
        else{
            identify=1;
            userName=document.getElementById("user_name").value;
            password=document.getElementById("user_password").value;
            $.post("/User/Register2", "userName="+userName+"&userPassword="+password+"&identify="+identify, function (data,status) {
                if (data==0){
                    alert("注册失败");
                }
                else if(data==2){
                    alert("请从头开始注册");
                }
                else{
                    window.location.href = "setUs.jsp";
                }
            },"json");
        }
    }
}


/*$("#get_captcha").click(function () {
    var userAccount=$("#userAccount").val();
    $.post("/user/PCaptcha", "userAccount="+userAccount, function () {

    });
});
$("#register").click(function (){
    var userAccount=$("#userAccount").val();
    var captcha = $("#captcha").val();
    $.post("/User/Register", "userAccount="+userAccount+"&captcha="+captcha, function () {
        if (data==0){
            window.location.href = "register2.html";
        } else if (data==1){

        }else if (data==2){

        }else if(data==3){

        }
    });
});*/
